package com.jdk21.core.model;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Data
public class DecoSerialCodeDataListener extends AnalysisEventListener<DecoSerialCodeData> {
    private List<DecoSerialCodeData> decoSerialCodeDataList = new ArrayList<>(500);
    private String message;
    private Boolean success;

    public DecoSerialCodeDataListener() {
        this.success = true;
    }

    @Override
    public void invoke(DecoSerialCodeData decoSerialCodeData, AnalysisContext analysisContext) {
        if (!success) {
            return;
        }
        Integer rowIndex = analysisContext.readRowHolder().getRowIndex();
        log.info("rowIndex:{}", rowIndex);

        String serialCode = decoSerialCodeData.getSerialCode();
        String rfid = decoSerialCodeData.getRfid();

        if (StringUtils.isBlank(serialCode) && StringUtils.isBlank(rfid)) {
            log.warn("rowIndex：{} serialCode和rfid都为空，忽略", rowIndex);
            return;
        }

        if (StringUtils.isBlank(serialCode)) {
            success = false;
            message = String.format("第%d行：serialCode为空", rowIndex + 1);
            return;
        }
        if (StringUtils.isBlank(rfid)) {
            success = false;
            message = String.format("第%d行：RFID为空", rowIndex + 1);
            return;
        }
        decoSerialCodeDataList.add(decoSerialCodeData);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

}